package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.PlayList;
import org.example.musicserver.entity.Singer;
import org.example.musicserver.entity.Song;
import org.example.musicserver.entity.VO.ListVO;
import org.example.musicserver.mapper.PlayListMapper;
import org.example.musicserver.mapper.SingerMapper;
import org.example.musicserver.mapper.SongMapper;
import org.example.musicserver.service.SongService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {
    @Resource
    private SongMapper songMapper;

    @Resource
    private SingerMapper singerMapper;

    @Resource
    private PlayListMapper playListMapper;


    @Override
    public Result songOfId(Integer id) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return Result.success(null, songMapper.selectOne(queryWrapper));
    }
    @Override
    public List<Song> getAllSong(String query){
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        if(query != null){
            queryWrapper.like("name",query);
        }
        return songMapper.selectList(queryWrapper);
    }

    @Override
    public Result songOfSingerId(Integer id) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("singer_id",id);
        return Result.success(null, songMapper.selectList(queryWrapper));
    }
    @Override
    public Result songOfSingerIds(List<Integer> ids) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids).orderByDesc("create_time");
        return Result.success(null, songMapper.selectList(queryWrapper));
    }

    @Override
    public Result selectList(String query) {
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        songQueryWrapper.like("name",query).last("limit 10");
        List<Song> songs = songMapper.selectList(songQueryWrapper);

        QueryWrapper<Singer> singerQueryWrapper = new QueryWrapper<>();
        singerQueryWrapper.like("name",query).last("limit 10");
        List<Singer> singers = singerMapper.selectList(singerQueryWrapper);

        QueryWrapper<PlayList> playListQueryWrapper = new QueryWrapper<>();
        playListQueryWrapper.like("title",query).last("limit 10");
        List<PlayList> playLists = playListMapper.selectList(playListQueryWrapper);

        ListVO listVO = new ListVO();
        listVO.setSongList(songs);
        listVO.setSingerList(singers);
        listVO.setPlayLists(playLists);

        return Result.success("返回结果成功",listVO);

    }

    @Override
    public Result addSong(Song song) {
        if(song.getSingerId() == null){
            return Result.error("歌手id不能为空");
        }
        return Result.success("添加成功",songMapper.insert(song));
    }

    @Override
    public Result updateSong(Song song) {
        if(song.getId() == null){
            return Result.error("id不能为空");
        }
        return Result.success("修改成功",songMapper.updateById(song));
    }

    @Override
    public Result deleteSong(Integer id) {
        if(id == null){
            return Result.error("id不能为空");
        }
        return Result.success("删除成功",songMapper.deleteById(id));
    }
}
